const path = require('path')
const {fileUploadError,unSupportedFileType,publicGoodsError,invalidGoodsId} = require('../constant/err.type')

const {createGoods,updateGoods,removeGoods,restoreGoods,findGoods} = require('../service/goods.service')
class GoodsController {
    async upload(ctx,next){
        
        // console.log(ctx.request.files.file);
        const {file} = ctx.request.files

        const fileTypes = ['image/jpeg','image/png']
        
        if(file){
            if(!fileTypes.includes(file.mimetype)){
                return ctx.app.emit('error',unSupportedFileType,ctx)
            }
            ctx.body={
                code:0,
                message:'图片上传成功',
                result:{
                    goods_img:path.basename(file.filepath)
                }
            }
        }else{
            return ctx.app.emit('error',fileUploadError,ctx)
        }
    }
    async create (ctx){
        //直接调用servi中的createGoods方法
        try {
            const {createdAt,updatedAt,...res} = await createGoods(ctx.request.body)
            ctx.body = {
                code:0,
                message:'发布商品成功',
                result:res
            }
            
        } catch (error) {
            console.log(error);
            return ctx.app.emit('error',publicGoodsError,ctx)
        }

    }
    async update(ctx){
        try {
            
            const res = await updateGoods(ctx.params.id,ctx.request.body)
    
            if(res){
                ctx.body = {
                    code:0,
                    message:'商品修改成功',
                    result:''
                }
            }else{
                return ctx.app.emit('error',invalidGoodsId,ctx)
            }
        } catch (error) {
            console.log(error);
        }
    }

    async remove(ctx){
        try {
            
            const res = await removeGoods(ctx.params.id)
            // console.log(res);
            if(res){
                ctx.body = {
                    code:0,
                    message:'下架商品成功',
                    result:''
                }
            }else{
                return ctx.app.emit('error',invalidGoodsId,ctx)
            }
        } catch (error) {
            console.log(error);
        } 
    }

    async restore(ctx){
        const res = await restoreGoods(ctx.params.id)
        if(res){
            ctx.body = {
                code:0,
                message:'上架商品成功',
                result:''
            }
        }else{
            return ctx.app.emit('error',invalidGoodsId,ctx)
        }
    }

    async findAll(ctx){
        try {
            
            const {pageNum=1, pageSize=10} = ctx.request.query
    
            const res = await findGoods({pageNum,pageSize})
    
            ctx.body = {
                code:0,
                message:'获取商品成功',
                result:res
            }
        } catch (error) {
            console.log(error);
            ctx.body = {
                code:0,
                message:'获取商品失败',
                result:''
            }
        }
    }

}

module.exports = new GoodsController()